package com.report.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entity.Report_electri_Power;
import com.entity.Report_water_use;
import com.interfaces.CustomerMeterDao;
import com.interfaces.MeterDao;
import com.interfaces.Report_water_useDao;
import com.interfaces.TaskRecordItemDao;
import com.report.entity.JsonTaskRecordReport;
import com.utils.CglibBeanCopierUtils;
import com.utils.JsonResult;

/**
 * @Title: TaskRecordReport_water_Service.java
 * @Package com.report.service
 * @author 孙梦楠
 * @date 2017年10月31日 下午2:47:03
 * @version V1.0.0
 */
@Service
public class TaskRecordReport_water_Service {

	@Autowired
	private TaskRecordItemDao taskrecorditemdao;

	@Autowired
	private CustomerMeterDao customermeterdao;

	@Autowired
	private MeterDao meterdao;

	@Autowired
	private Report_water_useDao reportwaterdao;

	@Autowired
	private TaskRecordReport_ele_Service TaskRecordReport_ele_service;

	// 通过表计查总报表 曲线?1
	@Transactional

	public JsonResult<Object[][]> recordreportLine(Integer customerid, String code, String time1, String time2,
			String status2) {

		if (customermeterdao.findByCustomerid2(customerid) == null) {
			return null;
		} else {
			String begintime = null;
			String endtime = null;
			if (time1 == null) {
				begintime = null;
			} else {
				begintime = time1 + " 00:00:00";
			}
			if (time2 == null) {
				endtime = null;
			} else {
				endtime = time2 + " 00:00:00";

			}
			Integer meterid = customermeterdao.findByCustomerid2(customerid).getPk().getMeterid();
			String meteraddress = meterdao.findOne(meterid).getMeteraddress();

			List<Report_water_use> taskRecordItems = reportwaterdao.findTaskrecorditemByMeter(meteraddress, code,
					begintime, endtime);

			Object[][] obj = new Object[taskRecordItems.size()][2];
			int i = 0;

			for (Report_water_use taskRecordItem : taskRecordItems) {
				String time = taskRecordItem.getTime();
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
				Date date = null;
				try {
					date = formatter.parse(time);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				obj[i][0] = date.getTime() + 28800000;
				obj[i][1] = Double.valueOf(taskRecordItem.getValue1());
				i++;
			}
			
			return new JsonResult<Object[][]>(681, "所有报表_曲线", obj);

		}
	}

	// 通过表计查总报表 表格
	@Transactional
	public JsonResult<List<JsonTaskRecordReport>> recordreportTable(Integer customerid, String code, String time1,
			String time2, String status2) {

		if (customermeterdao.findByCustomerid2(customerid) == null) {
			return new JsonResult<List<JsonTaskRecordReport>>(682, "用户不存在", null);
		} else {
			Integer meterid = customermeterdao.findByCustomerid2(customerid).getPk().getMeterid();
			String meteraddress = meterdao.findOne(meterid).getMeteraddress();

			List<Report_water_use> taskRecordItems = reportwaterdao.findTaskrecorditemByMeter(meteraddress, code, time1,
					time2);
			List<JsonTaskRecordReport> taskRecordItems1 = new ArrayList<JsonTaskRecordReport>();
			for (Report_water_use taskRecordItem : taskRecordItems) {
				taskRecordItem.setValue1(Double.parseDouble(taskRecordItem.getValue1()) + "");
				JsonTaskRecordReport report = new JsonTaskRecordReport();
				CglibBeanCopierUtils.copyProperties(taskRecordItem, report);
				report.setValue1(taskRecordItem.getValue1());
				taskRecordItems1.add(report);
			}
			return new JsonResult<List<JsonTaskRecordReport>>(682, "所有报表_表格", taskRecordItems1);
		}
	}

	// 给时标和终端地址 查询前一天和后一天的列表
	public List<Report_water_use> listByTimeAndTerminaladdress_daybefore(String time, String terminaladdress,
			String code,String type) {
		String dayBefore = TaskRecordReport_ele_service.dayBefore(time,type);
		return reportwaterdao.findByTimeAndTerminaladdressAndCode(dayBefore, terminaladdress, code);
	}

	// 后一天
	public List<Report_water_use> listByTimeAndTerminaladdress_dayafter(String time, String terminaladdress,
			String code,String type) {
		String dayAfter = TaskRecordReport_ele_service.dayAfter(time,type);
		return reportwaterdao.findByTimeAndTerminaladdressAndCode(dayAfter, terminaladdress, code);
	}

	@Transactional
	public boolean delectdata(String terminaladdress, String time, String code,String type) {

		String code_use = code + "_use";
		String dayafter = TaskRecordReport_ele_service.dayAfter(time,type);
		reportwaterdao.deletedata(terminaladdress, time, code);
		reportwaterdao.deletedata(terminaladdress, time, code_use);
		reportwaterdao.deletedata(terminaladdress, dayafter, code_use);

		return true;
	}

}
